package utils;

import android.content.Context;

import java.util.Iterator;

import org.json.JSONException;
import org.json.JSONObject;

import com.lidroid.xutils.DbUtils;
import com.lidroid.xutils.exception.DbException;

/**
 * @Description TODO
 * @author 李文伟
 * @email lvsedewenzi@163.com
 * @date 2015-9-9 上午9:21:33
 */
public class DatabaseUtil {

	private  String VersionsJson = "{\"1\":{\"id\":\"INTEGER\",\"recordTimes\":\"INTEGER\",\"locationId\":\"INTEGER\",\"longitude\":\"REAL\",\"locationName\":\"TEXT\",\"latitude\":\"REAL\",\"hasSendRecord\":\"INTEGER\",\"nowTime\":\"TEXT\"},\"2\":{\"id\":\"INTEGER\",\"recordTimes\":\"INTEGER\",\"locationId\":\"INTEGER\",\"longitude\":\"REAL\",\"locationName\":\"TEXT\",\"latitude\":\"REAL\",\"hasSendRecord\":\"INTEGER\",\"nowTime\":\"TEXT\",\"speed\":\"REAL\"}}";
//	private  String WatchResultVersionsJson = "{\"1\":{\"id\":\"INTEGER\",\"recordTimes\":\"INTEGER\",\"locationId\":\"INTEGER\",\"longitude\":\"REAL\",\"locationName\":\"TEXT\",\"latitude\":\"REAL\",\"hasSendRecord\":\"INTEGER\",\"nowTime\":\"TEXT\"}}";

	public  void update( DbUtils dbUtils,String tableName, int oldVersion,int newVersion) {

		try {
			JSONObject jObject = new JSONObject(VersionsJson);
		
			JSONObject jold=jObject.optJSONObject(""+oldVersion);
			JSONObject jnew=jObject.optJSONObject(""+newVersion);
			

			
	        Iterator<String> jnewiterator = jnew.keys();
	        while(jnewiterator.hasNext()){
	                 String   newkey = (String) jnewiterator.next();
	               // value = jsonObject.getString(key);
	                 
	                 boolean isExitInOld=false;
	                 
	     	        Iterator<String> jolditerator = jold.keys();
	    	        while(jolditerator.hasNext()){
	    	                 String   oldkey = (String) jolditerator.next();
	    	               // value = jsonObject.getString(key);
	    	                 
	    	                 if (newkey.equals(oldkey)) {
	    	                	 isExitInOld=true;
	    	                	 break;
							}
	    	        }
	    	        
	    	        if (!isExitInOld) {
						LogUtils.e(newkey+"--NoExit--"+jnew.getString(newkey));
						String sqlString="alter table "+tableName+" add "+newkey+" "+jnew.getString(newkey);
						dbUtils.execNonQuery(sqlString);
						LogUtils.e(sqlString+"--AddSecuss");
					}
	        }
	        
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (DbException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		//LogUtil.i(jObject.toString());

	}

	public static DbUtils getDb(Context context, String dbDir, String dbName, int dbVersion, String updateTableName){

		DbUtils	locationDb = DbUtils.create(context,dbDir,dbName,dbVersion,
				new DbUtils.DbUpgradeListener() {
					@Override
					public void onUpgrade(DbUtils dbUtils, int oldVersion,
										  int newVersion) {
						// TODO Auto-generated method stub
						if (oldVersion < newVersion) {
							//  new DbUpdateUtil().update(dbUtils,updateTableName,oldVersion, newVersion);//升级数据库
						}
					}
				});
		locationDb.configAllowTransaction(true);//开启事务
		return locationDb;
	}

}
